package com.entor.hqj.main;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

import com.entor.hqj.login.LoginActivity;
import com.entor.hqj.login.R;
import com.entor.hqj.util.MoreTextView;
import com.entor.hqj.util.TitleBuilder;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class IndexActivity extends Activity {

    private MoreTextView mtv;
    private GridView gridView;
    private TextView tv_title;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_index);
        tv_title = (TextView)findViewById(R.id.tv_title);
        mtv = (MoreTextView)findViewById(R.id.expand_text_view);
        if(LoginActivity.userInfo!=null) {
            tv_title.setText(LoginActivity.userInfo.getName());
            String content = LoginActivity.userInfo.getContent();
            mtv.setText(content==null?"该主人很懒，没有简介":content);
        }

        gridView = (GridView) findViewById(R.id.gridView);
        initTitle();
    }

    public void initTitle(){
        /**
         * 1.设置左边的图片按钮不显示，以及事件
         * 2.设置中间TextView显示的文字
         * 3.设置右边的文本不显示，并设置事件
         */
        new TitleBuilder(this).setMidTitleText("应用主页")
        .setRightText("联系我们");

        addItems();

    }

    /**
     * 九宫格添加元素
     * 该元素的点击或者覆盖的背景在res-drawable-grid_selector_background.xml控制
     */
    public void addItems(){
        //声明存放元素的集合
        List<Map<String,Object>> items = new ArrayList<Map<String,Object>>();
        for(int i=0;i<3;i++){
            Map<String,Object> map = new HashMap<String,Object>();
            if(i==0){
                map.put("ItemImg",R.mipmap.umeng_socialize_ding);//添加图片
                //添加文本
                map.put("ItemText",getResources().getString(R.string.gridview1));
            }
            if(i==1){
                map.put("ItemImg",R.mipmap.umeng_socialize_copy);
                map.put("ItemText",getResources().getString(R.string.gridview2));
            }
            if(i==2){
                map.put("ItemImg",R.mipmap.umeng_socialize_copyurl);
                map.put("ItemText",getResources().getString(R.string.gridview3));
            }
            items.add(map);
        }
        //添加到适配器
        SimpleAdapter sa =
                new SimpleAdapter(this,
                        items,
                        R.layout.grid_item,//九宫格显示文本的模版
                        new String[]{"ItemImg","ItemText"},
                        new int[] {R.id.ItemImg,R.id.ItemText});

        //设置到视图
        gridView.setAdapter(sa);
        //设置九宫格监听事件
        gridView.setOnItemClickListener(new ItemClickListener());
    }

    //内部内部类添加事件监听
    class ItemClickListener implements AdapterView.OnItemClickListener{

        @Override
        public void onItemClick(
                AdapterView<?> adapterView, //点击AdapterView产生事件
                View view,//得到是点击了哪一个图片或视图
                int i,//点击视图的位置
                long l//点击视图的行数
        ) {
            Map<String,Object> items = (Map<String, Object>) adapterView.getItemAtPosition(i);
            String itemText = (String)items.get("ItemText");
            if(itemText.equals(getResources().getString(R.string.gridview1))){
                Intent intent = new Intent(IndexActivity.this,ListViewActivity.class);
                startActivity(intent);
            }
            if(itemText.equals(getResources().getString(R.string.gridview2))){
                Intent intent=new Intent(IndexActivity.this,TablayoutActivity.class);
                intent.putExtra("type",0);
                startActivity(intent);
            }
            if(itemText.equals(getResources().getString(R.string.gridview3))){
                Intent intent = new Intent(IndexActivity.this,SetupActivity.class);
                startActivity(intent);
            }
        }
    }
}
